import Vue from 'vue'
import * as types from '../mutation-types'
import utils from '../../utils/common'
import { baseApi } from '@/api'
import router from '../../router'

export default {
    state: {
        currentChatId: 0,
        currntGroupSubs: null,
        chatList: [],
        chatInputObj: {},
        isReplyMsg: false,
        // seq自增
        seqAutoInc: 1,
        currentTab: 'message',
        currentGroupTab: 'myself'
    },
    mutations: {
        [types.CURRENT_TAB](state, res) {
            state.currentTab = res
        },
        [types.CURRENT_GROUP_TAB](state, res) {
            state.currentGroupTab = res
        },
        [types.UPDATE_IS_REPLY_MSG](state, res) {
            state.isReplyMsg = res
        },
        [types.SEQ_AUTO_INC](state, res) {
            state.seqAutoInc = res
        },
        [types.CREATE_CHAT_LIST](state, res) {
            let index = utils.chartListFilter(res.id, state.chatList)
            state.currentChatId = res.id
            if (index == -1) {
                state.chatList.unshift(res)
            } else {
                let filter = state.chatList[index]
                state.chatList.splice(index, 1)
                state.chatList.unshift(filter)
            }
        },
        [types.INSERT_CHAT_LIST](state, res) {
            state.chatList.unshift(res)
        },
        [types.INSERT_CHAT_INPUT](state, obj) {
            if (!state.chatInputObj[obj.id]) {
                state.chatInputObj[obj.id] = obj
            }
        },
        [types.MODIFY_CHAT_INPUT_ATLIST](state, obj) {
            if (state.chatInputObj[obj.id]) {
                state.chatInputObj[obj.id].atUserList = obj.list
                // vue computed无法监听属性值变化
                let newObj = Object.assign({}, state.chatInputObj)
                state.chatInputObj = newObj
            }
        },
        [types.MODIFY_CHAT_INPUT_CONTENT](state, obj) {
            if (state.chatInputObj[obj.id]) {
                state.chatInputObj[obj.id].content = obj.content
                // vue computed无法监听属性值变化
                let newObj = Object.assign({}, state.chatInputObj)
                state.chatInputObj = newObj
            }
        },
        [types.SET_CURRENT_CHAT](state, id) {
            let index = utils.chartListFilter(id, state.chatList)
            state.currentChatId = id
            state.chatList[index].count = 0
        },
        [types.SAVE_PERSON_MESSAGE](state, res) {
            // lahei or delete by friend
            if (res.stage == -3) {
                let tipsMsg = res.tipsMsg
                let obj = utils.chartCurrMsgByMsgId(res.msgId, state.chatList)
                res = obj.msgObj
                if (!state.chatList[obj.index].msgData) {
                    return
                }
                var findIndex = state.chatList[obj.index].msgData.findIndex(el => { return el.msgId == res.msgId })
                if (findIndex != -1) {
                    state.chatList[obj.index].msgData.splice(findIndex, 1, {
                        stage: -3,
                        msg: res.msg,
                        date: res.date,
                        contentType: res.contentType,
                        senderid: res.senderid,
                        msgId: res.msgId,
                        destroy: res.destroy,
                        own: res.own ? res.own : 0,
                        tipsMsg: tipsMsg,
                        // 回复消息才有以下字段
                        scExpand: res.scExpand,
                        ellipsis: res.ellipsis,
                        ellipsisSc: res.ellipsisSc
                    })
                }
                return
            }
            let index = utils.chartListFilter(res.senderid, state.chatList)
            if (res.contentType == 55) {
                let msgData = state.chatList[index].msgData
                for (let i = 0; i < msgData.length; i++) {
                    if (msgData[i].msgId == parseInt(res.msgId)) {
                        state.chatList[index].msgData[i].contentType = 55
                    }
                }
            } else {
                if (res.senderid != state.currentChatId) {
                    if (res.own != 1) {
                        state.chatList[index].count += 1
                    }
                } else {
                    state.chatList[index].count = 0
                }
                var findIndex = state.chatList[index].msgData.findIndex(el => { return el.msgId == res.msgId })
                // console.error('findIndex=' + findIndex)
                if (findIndex == -1) {
                    state.chatList[index].msgData.push({
                        stage: res.stage,
                        msg: res.msg,
                        date: res.date,
                        contentType: res.contentType,
                        senderid: res.senderid,
                        msgId: res.msgId,
                        destroy: res.destroy,
                        own: res.own ? res.own : 0,
                        // 回复消息才有以下字段
                        scExpand: res.scExpand,
                        ellipsis: res.ellipsis,
                        ellipsisSc: res.ellipsisSc
                    })
                // 替换消息
                } else {
                    // TODO
                    // if (res.stage == 3) {
                    //     return
                    // }
                    // TODO
                    state.chatList[index].msgData.splice(findIndex, 1, {
                        stage: res.stage,
                        msg: res.msg,
                        date: res.date,
                        contentType: res.contentType,
                        senderid: res.senderid,
                        msgId: res.msgId,
                        destroy: res.destroy,
                        own: res.own ? res.own : 0,
                        // 回复消息才有以下字段
                        scExpand: res.scExpand,
                        ellipsis: res.ellipsis,
                        ellipsisSc: res.ellipsisSc
                    })
                }
            }
            let filter = state.chatList[index]
            state.chatList.splice(index, 1)
            state.chatList.unshift(filter)
        },
        [types.SAVE_GROUP_MESSAGE](state, res) {
            let index = utils.chartListFilter(res.groupid, state.chatList)
            if (res.contentType == 55) {
                let msgData = state.chatList[index].msgData
                for (let i = 0; i < msgData.length; i++) {
                    if (msgData[i].msgId == parseInt(res.msgId)) {
                        state.chatList[index].msgData[i].contentType = 55
                    }
                }
            } else {
                if (res.groupid != state.currentChatId) {
                    if (res.senderid != res.userid) {
                        state.chatList[index].count += 1
                    }
                } else {
                    state.chatList[index].count = 0
                }
                var findIndex = state.chatList[index].msgData.findIndex(el => { return el.msgId == res.msgId })
                // 非web端自己发送的消息
                if (findIndex == -1) {
                    state.chatList[index].msgData.push({
                        stage: res.stage,
                        msg: res.msg,
                        date: res.date,
                        contentType: res.contentType,
                        senderid: res.senderid,
                        groupid: res.groupid,
                        msgId: res.msgId,
                        destroy: res.destroy,
                        own: res.own ? res.own : 0,
                        // 回复消息才有以下字段
                        scExpand: res.scExpand,
                        ellipsis: res.ellipsis,
                        ellipsisSc: res.ellipsisSc
                    })
                // 自己发送的消息更新为服务端的信息
                } else {
                    // TODO
                    // if (res.stage == 7) {
                    //     return
                    // }
                    // TODO
                    state.chatList[index].msgData.splice(findIndex, 1, {
                        stage: res.stage,
                        msg: res.msg,
                        date: res.date,
                        contentType: res.contentType,
                        senderid: res.senderid,
                        groupid: res.groupid,
                        msgId: res.msgId,
                        destroy: res.destroy,
                        own: res.own ? res.own : 0,
                        // 回复消息才有以下字段
                        scExpand: res.scExpand,
                        ellipsis: res.ellipsis,
                        ellipsisSc: res.ellipsisSc
                    })
                }
            }
            let filter = state.chatList[index]
            state.chatList.splice(index, 1)
            state.chatList.unshift(filter)
        },
        [types.SET_CURRENT_GROUP_SUBS](state, res) {
            state.currntGroupSubs = res
        },
        [types.DEL_CHAT_LIST_ITEM](state, id) {
            let index = utils.chartListFilter(id, state.chatList)
            state.chatList.splice(index, 1)
            state.currentChatId = 0
        },
        [types.RESET_CHAT](state) {
            state.currentChatId = 0
            state.currntGroupSubs = null
            state.chatList = []
        },
        [types.SET_CHAT_STATUS](state, res) {
            for (let i = 0; i < state.chatList.length; i++) {
                if (state.chatList[i].id == parseInt(res.id)) {
                    state.chatList[i].status = res.status
                }
            }
        }
    },
    actions: {
        // 保存聊天输入框内容和信息
        insertChatInput({ commit }, obj) {
            commit('INSERT_CHAT_INPUT', obj)
        },
        modifyChatInputAtList({ commit }, obj) {
            commit('MODIFY_CHAT_INPUT_ATLIST', obj)
        },
        modifyChatInputContent({ commit }, obj) {
            commit('MODIFY_CHAT_INPUT_CONTENT', obj)
        },
        initSocket() {
        },
        createChat({ commit }, res) {
            commit('CREATE_CHAT_LIST', res)
        },
        updateIsReplyMsg({ commit }, res) {
            commit('UPDATE_IS_REPLY_MSG', res)
        },
        updateCurrentTab({ commit }, res) {
            commit('CURRENT_TAB', res)
        },
        updateCurrentGroupTab({ commit }, res) {
            commit('CURRENT_GROUP_TAB', res)
        },
        updateSeqAutoInc({ commit }, res) {
            commit('SEQ_AUTO_INC', res)
        },
        insertChat({ commit }, res) {
            commit('INSERT_CHAT_LIST', res)
        },
        setCurrentChat({ commit }, id) {
            commit('SET_CURRENT_CHAT', id)
        },
        savePersonMessage({ commit }, res) {
            // lahei or delete by friend
            if (res.stage == -3) {
                commit('SAVE_PERSON_MESSAGE', res)
                return
            }
            if (res.contentType == 1021 || res.contentType == 1012) {
                let msg = res.msg
                msg = JSON.parse(msg)
                if (msg.bigUrl) {
                    baseApi.readTxt({ url: msg.bigUrl }).then(rsp => {
                        if (rsp.code == 200) {
                            if (res.contentType == 1021) {
                                msg.content = rsp.data
                            } else {
                                msg.sc = JSON.parse(rsp.data).sc
                                msg.rc = JSON.parse(rsp.data).rc
                                // 50个字符以上 展开收缩显示
                                if (msg.sc.length > 50) {
                                    // 标记展开还是收缩状态
                                    res.scExpand = false
                                    res.ellipsis = true
                                    res.ellipsisSc = msg.sc.substring(0, 50) + '...'
                                }
                            }
                            res.msg = JSON.stringify(msg)
                            commit('SAVE_PERSON_MESSAGE', res)
                        } else {
                            commit('SAVE_PERSON_MESSAGE', res)
                        }
                    })
                } else {
                    if (res.contentType == 1012) {
                        let msg = res.msg
                        msg = JSON.parse(msg)
                        if (msg.sc.length > 50) {
                            // 标记展开还是收缩状态
                            res.scExpand = false
                            res.ellipsis = true
                            res.ellipsisSc = msg.sc.substring(0, 50) + '...'
                        }
                    }
                    commit('SAVE_PERSON_MESSAGE', res)
                }
            } else {
                commit('SAVE_PERSON_MESSAGE', res)
            }
        },
        saveGroupMessage({ commit }, res) {
            if (res.contentType == 1021 || res.contentType == 1012) {
                let msg = res.msg
                msg = JSON.parse(msg)
                if (msg.bigUrl) {
                    baseApi.readTxt({ url: msg.bigUrl }).then(rsp => {
                        if (rsp.code == 200) {
                            if (res.contentType == 1021) {
                                msg.content = rsp.data
                            } else {
                                msg.sc = JSON.parse(rsp.data).sc
                                msg.rc = JSON.parse(rsp.data).rc
                                // 50个字符以上 展开收缩显示
                                if (msg.sc.length > 50) {
                                    // 标记展开还是收缩状态
                                    res.scExpand = false
                                    res.ellipsis = true
                                    res.ellipsisSc = msg.sc.substring(0, 50) + '...'
                                }
                            }
                            res.msg = JSON.stringify(msg)
                            commit('SAVE_GROUP_MESSAGE', res)
                        } else {
                            commit('SAVE_GROUP_MESSAGE', res)
                        }
                    })
                } else {
                    if (res.contentType == 1012) {
                        let msg = res.msg
                        msg = JSON.parse(msg)
                        if (msg.sc.length > 50) {
                            // 标记展开还是收缩状态
                            res.scExpand = false
                            res.ellipsis = true
                            res.ellipsisSc = msg.sc.substring(0, 50) + '...'
                        }
                    }
                    commit('SAVE_GROUP_MESSAGE', res)
                }
            } else {
                commit('SAVE_GROUP_MESSAGE', res)
            }
        },
        setCrrentGroupSubs({ commit }, res) {
            commit('SET_CURRENT_GROUP_SUBS', res)
        },
        delChatListItem({ commit }, id) {
            commit('DEL_CHAT_LIST_ITEM', id)
        },
        resetChat({ commit }) {
            commit('RESET_CHAT')
        },
        setChatStatus({ commit }, res) {
            commit('SET_CHAT_STATUS', res)
        }
    }
}
